package com.itheima.exception;

import com.itheima.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常捕获
 *
 * @author liudo
 * @version 1.0
 * @project tlias-backend
 * @date 2023/9/12 17:10:29
 */
@Slf4j
// 全局异常捕获类
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 运行时异常处理程序
     *
     * @param exception 例外
     * @return {@link Result}<{@link ?}>
     */
    @ExceptionHandler(RuntimeException.class)
    public Result<?> runtimeExceptionHandler(RuntimeException exception) {
        log.error("异常捕获到:{}", exception.getMessage());
        return Result.error(exception.getMessage());
    }

    /**
     * 异常处理程序
     *
     * @param exception 例外
     * @return {@link Result}<{@link ?}>
     */
    @ExceptionHandler(Exception.class)
    public Result<?> exceptionHandler(Exception exception) {
        log.error("异常捕获到:{}", exception.getMessage());
        return Result.error("系统繁忙，请稍后重试~");
    }
}
